import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import stringify from 'Shared/utils/stringify';
import {formatMessage} from './intl';

const baseUrl = '/afterSales/api/v1';
const resourceName = formatMessage({
    id: 'api.resourceName',
    defaultMessage: '营销活动申报'
});
const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询{resourceName}'
}, {resourceName});

const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询{resourceName}详情'
}, {resourceName});

const updateDataDesc = formatMessage({
    id: 'api.message.updateDataDesc',
    defaultMessage: '编辑{resourceName}'
}, {resourceName});

const getAddDesc = formatMessage({
    id: 'api.getAdd',
    defaultMessage: '新增{resourceName}'
}, {resourceName});


const abandonDataDesc = formatMessage({
    id: 'api.message.abandonDataDesc',
    defaultMessage: '作废{resourceName}'
}, {resourceName});

const approveDataDesc = formatMessage({
    id: 'api.message.approveDataDesc',
    defaultMessage: '审核{resourceName}'
}, {resourceName});

const submitDataDesc = formatMessage({
    id: 'api.message.submitDataDesc',
    defaultMessage: '提报{resourceName}'
}, {resourceName});

const takeEffectDataDesc = formatMessage({
    id: 'api.message.takeEffectDataDesc',
    defaultMessage: '生效{resourceName}'
}, {resourceName});

const getPassDesc = formatMessage({
    id: 'api.getPassDesc',
    defaultMessage: '通过{resourceName}'
}, {resourceName});

const getRejectDesc = formatMessage({
    id: 'api.getRejectDesc',
    defaultMessage: '拒绝{resourceName}'
}, {resourceName});

const getRejectToPrevDesc = formatMessage({
    id: 'api.getRejectToPrevDesc',
    defaultMessage: '驳回{resourceName}'
}, {resourceName});

// 初始化
export const getInitData = () => request(`${baseUrl}/ui/marketingActivityDeclare/init`, null, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getListDesc));

// 查询订单列表
export const getList = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/marketingActivityDeclares?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
// 查询详情
export const getDetail = id =>
    request(`${baseUrl}/marketingActivityDeclares/${id}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));
// 修改
export const updateData = (id, data) => request(`${baseUrl}/marketingActivityDeclares/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateDataDesc));

//新增
export const newOrder = data => request(`${baseUrl}/marketingActivityDeclares`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getAddDesc));

// 作废
export const abandonData = (id, data) => request(`${baseUrl}/marketingActivityDeclares/${id}/abandon`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, abandonDataDesc));

// 审核
export const approveData = (id, data) => request(`${baseUrl}/marketingActivityDeclares/${id}/approve`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, approveDataDesc));

// 提报
export const submitData = (id, data) => request(`${baseUrl}/marketingActivityDeclares/${id}/submit`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, submitDataDesc));

// 提报
export const takeEffectData = (id, data) => request(`${baseUrl}/marketingActivityDeclares/${id}/takeEffect`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, takeEffectDataDesc));

// 通过
export const passData = (id, rowVersion) => request(`${baseUrl}/marketingActivityDeclares/${id}/pass`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(rowVersion)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getPassDesc));

// 拒绝
export const rejectData = (id, rowVersion) => request(`${baseUrl}/marketingActivityDeclares/${id}/reject`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(rowVersion)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getRejectDesc));

// 驳回
export const rejectToPrevData = (id, rowVersion) => request(`${baseUrl}/marketingActivityDeclares/${id}/rejectToPrev`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(rowVersion)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, getRejectToPrevDesc));

